package is.web.htgen.multiple;

import is.bean.department.DepartmentBeanRemoteHome;
import is.bean.employee.Employee;
import is.bean.employee.EmployeeBeanRemote;
import is.web.*;
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Iterator;
import javax.ejb.FinderException;

/**
 *
 * @author aspr0
 */
public class EmployeeTable
{
    private Collection<EmployeeBeanRemote> employees;
    private DepartmentBeanRemoteHome departmentHome;
    private Employee findExample;

    public void setDepartmentHome(DepartmentBeanRemoteHome departmentHome)
    {
        this.departmentHome = departmentHome;
    }

    public void setEmployees(Collection<EmployeeBeanRemote> employees)
    {
        this.employees = employees;
    }

    public void setFindExample(Employee findExample)
    {
        this.findExample = findExample;
    }

    public String generate() throws RemoteException, FinderException
    {
        String table =
                "<table>"
                + "<thead>%s</thead>"
                + "<tbody>%s%s</tbody>"
                + "</table>";
        String header =
                "<tr>"
                + "<td>Id</td>"
                + "<td>Last name</td>"
                + "<td>First name</td>"
                + "<td>Middle name</td>"
                + "<td>Department</td>"
                + "<td>Tools</td>"
                + "</tr>";
        String findId = NullUtil.toString(findExample.getId());
        String findLastName = NullUtil.toString(findExample.getLastName());
        String findFirstName = NullUtil.toString(findExample.getFirstName());
        String findMiddleName = NullUtil.toString(findExample.getMiddleName());
        String findForm =
                "<tr>"
                + "<form method=\"post\">"
                + "<td><input type=\"text\" name=\"id\" value=\"" + findId + "\" class=\"find-id\"></td>"
                + "<td><input type=\"text\" name=\"lastName\" value=\"" + findLastName + "\" class=\"find-wide\"></td>"
                + "<td><input type=\"text\" name=\"firstName\" value=\"" + findFirstName + "\" class=\"find-wide\"></td>"
                + "<td><input type=\"text\" name=\"middleName\" value=\"" + findMiddleName + "\" class=\"find-wide\"></td>"
                + "<td></td>"
                + "<td>"
                + "<input type=\"hidden\" name=\"find\" value=\"find\">"
                + "<input type=\"submit\" value=\"find\"></td>"
                + "</form>"
                + "</tr>";
        String body = "";


        Iterator<EmployeeBeanRemote> i = employees.iterator();

        while (i.hasNext())
        {
            Employee currentEmployee = i.next().getData();

            Integer id = currentEmployee.getId();
            String lastName = currentEmployee.getLastName();
            String firstName = currentEmployee.getFirstName();
            String middleName = currentEmployee.getMiddleName();
            Integer departmentId = currentEmployee.getDepartmentId();


            String departmentLinkCanvas = "<a href=\"../department?id=%s&act=retrieve\">%s</a>";
            String departmentLink = (Checker.isPositiveInteger(departmentId))
                    ? String.format(departmentLinkCanvas,
                                    departmentId,
                                    departmentHome.findByPrimaryKey(departmentId).getData().getName())
                    : Locale.NULL_READABLE;

            String row =
                    "<tr>"
                    + "<td>" + id + "</td>"
                    + "<td>" + lastName + "</td>"
                    + "<td>" +  firstName+ "</td>"
                    + "<td>" +  middleName+ "</td>"
                    + "<td>" + departmentLink+ "</td>"
                    + "<td>"
                    + "<a href=\"?id=" + id + "&act=retrieve\" title=\"retrieve\">"
                    + "<image src=\"../img/document-open.png\" class=\"hMargin\"></a>"
                    + "<a href=\"?id=" + id + "&act=update\" title=\"update\">"
                    + "<image src=\"../img/document-properties.png\" class=\"hMargin\"></a>"
                    + "<a href=\"?id=" + id + "&act=copy\" title=\"copy\">"
                    + "<image src=\"../img/edit-copy.png\" class=\"hMargin\"></a>"
                    + "<a href=\"?id=" + id + "&act=delete\" title=\"delete\">"
                    + "<image src=\"../img/edit-delete.png\" class=\"hMargin\"></a>"
                    + "</td>"
                    + "</tr>";
            body += row;
        }

        return String.format(table, header, findForm, body);

    }
}
